export function createRouteMap (routes, oldPathMap) {
  // 1个参数就是初始化，2 个参数就是 动态添加路由

  // 路径与记录的映射表
  let pathMap = oldPathMap || {};

  // 对 routes 进行递归
  routes.forEach (route => {
    addRouteRecord (route, pathMap, null); // pathMap = {路径:记录}
  });
  return {
    pathMap,
  };
}

function addRouteRecord (route, pathMap, parent) {
  // 如果 child 的 path 是以 / 开头，就会认为是一级路由 ，子路由的 path 不能以 / 开头
  let path = parent ? parent.path + '/' + route.path : route.path;
  let record = {
    path,
    parent,
    component: route.component,
    name: route.name || '',
    props: route.props,
    params: route.params,
    meta: route.meta,
  };
  if (!pathMap[path]) {
    pathMap[path] = record;
  }
  // 如果有嵌套路由就递归处理
  if (route.children) {
    route.children.forEach (childRoute => {
      addRouteRecord (childRoute, pathMap, record);
    });
  }
}
